home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Editorial / Programming with Delphi 2005 / Delphi 2005 1 / CALCTEST / CALCFORM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2005-02-21  |  2.1 KB  |  86 lines

  1. unit calcform;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, XPMan;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     CalcBtn: TButton;
  12.     GrandTotal: TEdit;
  13.     Vat: TEdit;
  14.     SubTotal: TEdit;
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     Label3: TLabel;
  18.     procedure CalcBtnClick(Sender: TObject);
  19.   private
  20.     { Private declarations }
  21.   public
  22.     { Public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.DFM}
  31.  
  32.  
  33.  
  34. procedure TForm1.CalcBtnClick(Sender: TObject);
  35. // all variables must be declared in the VAR section
  36. // syntax is:
  37. //           < variable name > : < type > ;
  38. // multiple variables of the same type may
  39. // be separated by commas
  40. var
  41.    stxt, vtxt,
  42.        gtxt : string;
  43.    st, vt, gt  : real;
  44.    errcode : integer;
  45. begin
  46. // First, let's initialise the variables to 0.0 for the
  47. // floating point numbers and empty strings, '', for the
  48. // strings
  49.    st := 0.0;
  50.    vt := 0.0;
  51.    gt := 0.0;
  52.    stxt := '';
  53.    vtxt := '';
  54.    gtxt := '';
  55.    begin // in Pascal code blocks are delimited by the keywords
  56.          // BEGIN and END - these are like the curly braces {}
  57.          // in C-like languages and Java
  58.  
  59. // try to convert SubTotal.Text to the real value, st.
  60. // if there is an error, errorcode returns the index
  61. // of the error in the string
  62.        Val(SubTotal.Text, st, errcode);
  63. // Use errorcode to index into the input string in order to
  64. // display the problem character
  65.        if errcode <> 0 then
  66.           ShowMessage('Character #'+IntToStr(errcode)+' "'
  67.             +SubTotal.Text[errcode]+'" is invalid.')
  68.        else
  69.        begin
  70.           vt := (st * 0.175);
  71.           gt := vt + st;
  72. // Instead of the FloatToStr() functions you could use
  73. // the Str() procedure which allows you to set a # of decimal
  74. // places - here 2
  75. //          Str(vt:2:2, vtxt);
  76. //          Str(gt:2:2, gtxt);
  77.           vtxt := FloatToStr(vt);
  78.           gtxt := FloatToStr(gt);
  79.           Vat.Text := vtxt;
  80.           GrandTotal.Text := gtxt;
  81.        end
  82.    end
  83. end;
  84.  
  85. end.
  86.